// pages/bindPhone/bindPhone.js
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    placeholder: '',
    inputPhone: '',
    inputCode: '',
    codeTitle: '获取',
    timer: '', //定时器
    disAbled: true, //是否可以获取验证码标识  默认true
  },

  //提交
  commit: function() {
    var that = this;
    if (that.data.inputPhone.length > 0 && that.data.inputCode.length > 0) {
      if (that.data.phone.length > 0) {
        //变更手机号  
        if (that.data.inputPhone == that.data.phone) {
          wx.showToast({
            title: '您的手机号未发生变更，请使用新号码',
            icon: 'none'
          })
        } else {
          that.changePhone(util.UpdatePwd)
        }
      } else {
        //首次绑定手机号
        that.changePhone(util.BindWeiXinPhone)
      }
    } else {
      wx.showToast({
        title: '请输入手机号，验证码',
        icon: 'none'
      })
    }
  },
  //绑定手机号 变更手机号
  changePhone: function(url) {
    var that = this;
    var dic = {
      phone: that.data.inputPhone,
      verify_code: that.data.inputCode
    }
    util.publicRequestWithToken(
      url,
      'POST',
      dic,
      //sucess
      function(data) {
        console.log('绑定成功', data)
        util.userPhone = that.data.inputPhone
        //返回
        wx.navigateBack({
          delta: 1
        })
      },
      //fail
      function(error) {},
      function() {}
    );
  },
  getCode: function() {
    var that = this
    if (that.data.disAbled) {
      if (that.data.inputPhone.length == 0) {
        wx.showToast({
          title: '请输入手机号',
          icon: 'none'
        })
      } else {
        that.togetCode()
      }
    }

  },

  //重新获取验证码
  togetCode: function() {
    var that = this;
    var dic = {
      "phone": that.data.inputPhone
    }
    util.publicRequestWithToken(
      util.GetCode,
      'GET',
      dic,
      //sucess
      function(data) {
        wx.showToast({
          title: '验证码已发送到你的手机上',
          icon: 'none'
        })
        that.setData({
          disAbled: false
        })
        //倒计时
        that.countDown()
      },
      //fail
      function(error) {
        wx.showToast({
          title: error.detail.msg,
        })
      }
    );
  },
  //验证码60秒倒计时
  countDown: function() {
    let that = this;
    let countDownNum = 60; //获取倒计时初始值
    that.setData({
      timer: setInterval(function() {
        countDownNum--;
        that.setData({
          codeTitle: countDownNum + '秒'
        })
        if (countDownNum == 0) {
          clearInterval(that.data.timer);
          //关闭定时器
          that.setData({
            codeTitle: '获取',
            disAbled: true
          })
        }
      }, 1000)
    })
  },
  getinputphone: function(e) {
    this.setData({
      inputPhone: e.detail.value
    })
  },
  getinputcode: function(e) {
    this.setData({
      inputCode: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      phone: options.phone,
    })
    var title = options.phone.length > 0 ? '手机号变更' : '绑定手机号'
    this.setData({
      placeholder: options.phone.length > 0 ? '请输入新的手机号' : '输入手机号'
    })
    //标题
    wx.setNavigationBarTitle({
      title: title
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})